package com.gen.generator.ui;

import com.intellij.openapi.fileChooser.FileChooser;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

public class RightCodeGenDialog extends DialogWrapper {

    private JTextField authorField;
    private JTextField packageField;
    private JCheckBox tenantCheckBox;
    private JTextField outputField;
    private JButton browseButton;

    private Project project;

    public RightCodeGenDialog(Project project) {
        super(true); // use current window as parent
        this.project = project;
        init();
        setTitle("生成代码配置");
    }

    @Override
    protected @Nullable JComponent createCenterPanel() {
        JPanel panel = new JPanel(new GridBagLayout());
        panel.setPreferredSize(new Dimension(400, 200));
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(4, 4, 4, 4);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.weightx = 1;

        // 作者
        gbc.gridx = 0; gbc.gridy = 0;
        panel.add(new JLabel("作者:"), gbc);
        gbc.gridx = 1;
        authorField = new JTextField("author");
        panel.add(authorField, gbc);

        // 包路径
        gbc.gridx = 0; gbc.gridy = 1;
        panel.add(new JLabel("包路径:"), gbc);
        gbc.gridx = 1;
        packageField = new JTextField("com.example.demo");
        panel.add(packageField, gbc);

        // 是否多租户
        gbc.gridx = 0; gbc.gridy = 2;
        panel.add(new JLabel("是否多租户:"), gbc);
        gbc.gridx = 1;
        tenantCheckBox = new JCheckBox();
        tenantCheckBox.setSelected(true);
        panel.add(tenantCheckBox, gbc);

        // 输出路径
        gbc.gridx = 0; gbc.gridy = 3;
        panel.add(new JLabel("输出路径:"), gbc);
        gbc.gridx = 1;
        JPanel pathPanel = new JPanel(new BorderLayout());
        outputField = new JTextField(System.getProperty("user.home") + "\\Generated");
        browseButton = new JButton("浏览");
        pathPanel.add(outputField, BorderLayout.CENTER);
        pathPanel.add(browseButton, BorderLayout.EAST);
        panel.add(pathPanel, gbc);

        // 浏览按钮点击事件
        browseButton.addActionListener(this::onBrowse);
        return panel;
    }

    private void onBrowse(ActionEvent e) {
        FileChooserDescriptor descriptor = new FileChooserDescriptor(
                false, true, false, false, false, false
        );
        descriptor.setTitle("选择生成代码输出路径");
        var file = FileChooser.chooseFile(descriptor, project, null);
        if (file != null) {
            outputField.setText(file.getPath());
        }
    }

    public String getAuthor() {
        return authorField.getText();
    }

    public String getPackageName() {
        return packageField.getText();
    }

    public boolean isTenant() {
        return tenantCheckBox.isSelected();
    }

    public String getOutputPath() {
        return outputField.getText();
    }
}
